from flask import Flask, render_template, request, escape
from function import planning
from function import geocode
from function import getinfo

app = Flask(__name__)


@app.route('/process', methods=['POST'])
# 数据来源
def do_search() -> 'html':
    departure1 = request.form['Departure']
    destination1 = request.form['Destination']
    title = '结果:'
    departure2 = geocode(key='2d015ae1da99945790f1601af060c108', address=departure1)
    destination2 = geocode(key='2d015ae1da99945790f1601af060c108', address=destination1)
    results = planning(departure2, destination2)
    results = getinfo(results)
    with open('history.log', 'a') as f:
        print(request.form, request.user_agent, request.remote_addr, file=f, sep=" | ")

    return render_template('results.html',
                           the_title=title,
                           the_results=results,
                           the_departure=departure1,
                           the_destiantion=destination1)


@app.route('/')
@app.route('/entry')
def entry_page() -> 'html':
    title = '步行路径规划功能'
    return render_template('entry.html', the_title=title)


@app.route('/log')
def log_page() -> 'html':
    title = 'log日志'
    titles = ['起点和终点', '访问方式', '访问地址']
    data = []
    with open('history.log', encoding="gbk") as file:
        for i in file:
            data.append([])
            for j in i.split(" | "):
                data[-1].append(escape(j))
    return render_template('log.html',
                           the_title=title,
                           table_titles=titles,
                           the_data=data, )


if __name__ == '__main__':
    app.run(debug=True)
